////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.users.windows {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import kiwi.utils.Validation;
	
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.display.windows.BaseWindow;
	import plantool.events.UserEvent;
	import plantool.modules.users.windows.objects.UserListWindowUserRow;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class UserListWindow extends BaseWindow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _isUpdating:Boolean 	= false;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function UserListWindow() {
			super(false);
		}
		
		public override function dispose():void {
			//remove events
			userApi.removeEventListener(UserEvent.USER_UPDATED, onUsers_updated);
			
			//super dispose
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onError_query(result:QueryErrorResult):void {
			trace("Error in query: ", result.query);
			trace(" - ", result.message);
		}
		
		private function onComplete_getUsers(result:QueryResult):void {
			//show users
			if (Validation.isSet(result.data) && result.data.length > 0) {
				//create a row for each user
				for (var i:uint = 0; i < result.data.length; i++) {
					//create the user row
					var row:UserListWindowUserRow 	= new UserListWindowUserRow(this);
					row.user						= result.data[i];
					
					//position the row
					row.y 		= i * 20;
					row.width 	= width;
					
					//add the row
					addChild(row);
				}
			}
			
			//unset updating
			_isUpdating = false;
		}
		
		private function onUsers_updated(e:UserEvent):void {
			if (!_isUpdating) {
				//set updating
				_isUpdating = true;
				
				//clear all rows
				clearContent();
				
				//load new users
				userApi.getUsers(onComplete_getUsers, onError_query);
			}
		}
		
		protected override function init():void {
			//call super init
			super.init();
			
			//set title
			title = "Medewerkers";
			
			//go through all users and create rows for them
			userApi.addEventListener(UserEvent.USER_UPDATED, onUsers_updated);
			onUsers_updated(null);
		}
	}
}